package com.qmyx.supplychain.quartz.task;

import com.ytjj.qmyx.supplychain.common.configuration.LINKPlatformConfig;
import com.ytjj.qmyx.supplychain.common.configuration.LinkConfigObject;
import com.ytjj.qmyx.supplychain.common.enums.ProductStatusEnums;
import com.ytjj.qmyx.supplychain.mapper.BankerProductMapper;
import com.ytjj.qmyx.supplychain.mapper.ProductMapper;
import com.ytjj.qmyx.supplychain.mapper.ProductSpecMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerProduct;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProduct;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductSpec;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class ProductStockTask {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductSpecMapper productSpecMapper;
    @Resource
    private BankerProductMapper bankerProductMapper;
    @Resource
    private LINKPlatformConfig linkPlatformConfig;

    @Scheduled(cron = "0 0/3 * * * ?")
    private void updateProductStock() {
        log.info("===开始查询已上架的供应链商品,更新库存====");
        Example example = new Example(YxProduct.class);
        example.createCriteria().andEqualTo("status", ProductStatusEnums.UP.getStatus());
        List<YxProduct> productList = productMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(productList)) {
            return;
        }
        productList.stream().forEach(item -> {
            Integer stockSum = productSpecMapper.selectProductSpecStockByProductId(item.getId());
            if (!item.getStock().equals(stockSum)){
                item.setStock(stockSum);
                productMapper.updateByPrimaryKeySelective(item);
            }
        });
        log.info("===结束查询已上架的供应链商品,更新库存====");
    }

    /**
     * 获取link平台商品库存
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void getAndUpdateAbroadProductStock() {
        //link平台供应商id
//        List<Integer> linkBankerIds =
//                linkPlatformConfig.getLinks().stream().map(LinkConfigObject::getBankerId).collect(Collectors.toList());
//        if (CollectionUtils.isEmpty(linkBankerIds)) {
//            log.info("===linkPlatformConfig.bankerIds is null===");
//            return;
//        }
//        Example example = new Example(YxBankerProduct.class);
//        example.createCriteria()
//                .andIn("bankerId", linkBankerIds)
//                .andEqualTo("isExit", 1);
//        example.selectProperties("id", "bankerId");
//        List<YxBankerProduct> products = bankerProductMapper.selectByExample(example);
//        products.stream().forEach(item -> {
//
//        });
    }
}
